package java101;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Toolkit;

import java.util.Hashtable;



import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JSlider;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;



public class Sunflower extends JFrame

{

	public static void main(String[] args) {

		new Sunflower();

	}

	

	int maxSeeds = 1000;

	private Drawing drawing = new Drawing();

	private JSlider slider = new JSlider(0, maxSeeds, maxSeeds/50);

	

	public Sunflower() {

		this.setTitle("Angles");

		this.setSize(700, 700);

		this.setResizable(true);

		this.setLayout(new BorderLayout(10, 10));

		this.add(drawing, BorderLayout.CENTER);

		this.add(slider, BorderLayout.SOUTH);

		this.setVisible(true);

		Hashtable labels = slider.createStandardLabels(maxSeeds/10);

		slider.setPaintLabels(true);

		slider.setSnapToTicks(true);

		slider.setLabelTable(labels);

		slider.addChangeListener(new ChangeListener() {

			public void stateChanged(ChangeEvent e) {

				Sunflower.this.repaint();

			}

		});

	}



	@Override

	public void paintAll(Graphics g) {

		super.paintAll(g);

		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

		Dimension window = getSize();

		this.setLocation(screen.height / 2 - window.height / 2, screen.width / 2 - window.width / 2);

		

	}

	

	public class Drawing extends JPanel

	{

		public Drawing() {

		}

		

		@Override

		public void paint(Graphics g) {

			Dimension size = this.getSize();

			int xCenter = size.width / 2;

			int yCenter = size.height / 2;

			

			double phi = .5 * (1 + Math.sqrt(5));

			int numSeeds = slider.getValue();

			

			for (int i=0; i < numSeeds; i++)

			{

				double theta = i * phi * 2 * Math.PI;

				// r = 1.5, dotSize = 8

				double r = 1.5 * Math.sqrt(theta);

				Double x = r * Math.cos(theta) + xCenter;

				Double y = r * Math.sin(theta) + yCenter;

				g.setColor(Color.ORANGE);

				g.fillOval(x.intValue(), y.intValue(), 8, 8);

			}

		}

	}



}

